package com.lottery.material.controller.portal.report;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.pojo.report.ReportTypeDict;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.report.ReportTypeDictService;
import com.lottery.material.util.ExceptionUtil;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import com.lottery.material.vo.portal.report.ReportTypeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:    举报类型Controller
 * Author:         sunny
 * CreateDate:     2019-04-13 17:17
 */
@RequestMapping("/portal/report")
@Slf4j
@Controller
public class PortalReportTypeController extends BaseController {

    @Autowired
    private ReportTypeDictService reportTypeDictService;

    /**
     * description: 获取举报类型列表
     * author sunny
     * createDate 2019-04-13 下午 5:38
     * @param request
     * @return
     */
    @RequestMapping(value = "/getReportTypeList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getReportTypeList(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }
        String reportScope = reqParamMap.get("reportScope");    //1：用户；2：文章；
        if (StringUtils.isBlank(reportScope)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "reportScope");
        }else if(!"1".equals(reportScope) && !"2".equals(reportScope)){
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_ERROR, "reportScope");
        }

        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if(null != responseResult){
            return responseResult;
        }
        try {
            //根据举报范围查询
            List<ReportTypeDict> reportTypeList = reportTypeDictService.findByReportScope(Integer.valueOf(reportScope));

            //封装响应数据
            List<ReportTypeVo> reportTypeVos = new ArrayList<>();
            if(null != reportTypeList && reportTypeList.size() > 0){
                for(int i = 0; i < reportTypeList.size(); i++){
                    ReportTypeVo reportTypeVo = new ReportTypeVo();
                    ReportTypeDict reportTypeDict = reportTypeList.get(i);
                    reportTypeVo.setReportCode(reportTypeDict.getReportCode());
                    reportTypeVo.setReportName(reportTypeDict.getReportName());
                    reportTypeVos.add(reportTypeVo);
                }
            }
            return ResponseResult.buildSuccess(reportTypeVos);
        }catch (Exception e){
            log.error("【获取举报类型列表出现异常】：{}", ExceptionUtil.logExceptionStack(e));
            throw new CustomException(ResultEnum.GET_REPORT_TYPE_LIST_EXCEPTION);
        }
    }
}
